package com.zzyl.controller.manager;

import com.aliyun.iot20180120.models.QueryThingModelPublishedRequest;
import com.zzyl.base.PageBean;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.DeviceDto;
import com.zzyl.dto.DevicePageQueryDto;
import com.zzyl.service.DeviceService;
import com.zzyl.vo.DevicePropertyStatusVo;
import com.zzyl.vo.DeviceVo;
import com.zzyl.vo.ProductVo;
import com.zzyl.vo.QueryThingModelVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequiredArgsConstructor
@Api(tags = "Iot设备管理")
@RequestMapping("/iot")
public class DeviceController {

    final DeviceService deviceService;

    @ApiOperation("产品列表数据同步")
    @PostMapping("/syncProductList")
    public ResponseResult<Void> syncProductList(){
        log.info("产品列表数据同步");
        deviceService.syncProductList2Redis();
        return ResponseResult.success();
    }



    @ApiOperation("查询所有产品列表")
    @GetMapping("/allProduct")
    public ResponseResult<List<ProductVo>> getAllProductList(){
        log.info("查询所有产品列表");
        List<ProductVo> productVoList = deviceService.getAllProductList();
        return ResponseResult.success(productVoList);
    }



    @ApiOperation("注册设备")
    @PostMapping("/RegisterDevice")
    public ResponseResult<Void> registerDevice(@RequestBody DeviceDto dto){
        log.info("注册设备");
        deviceService.registerDevice(dto);
        return ResponseResult.success();
    }



    @ApiOperation("分页查询设备")
    @GetMapping("/pageQueryDevice")
    public ResponseResult<PageBean<DeviceVo>> pageQueryDevice(DevicePageQueryDto dto){
        log.info("分页查询设备:{}",dto);
        PageBean<DeviceVo> deviceDtoList = deviceService.pageQueryDevice(dto);
        return ResponseResult.success(deviceDtoList);
    }



    @ApiOperation("查询设备详情")
    @PostMapping("/QueryDeviceDetail")
    public ResponseResult<DeviceVo> queryDeviceDetail(@RequestBody DeviceDto dto){
        log.info("查询设备详情:{}",dto);
        DeviceVo deviceVo = deviceService.queryDeviceDetail(dto);
        return ResponseResult.success(deviceVo);
    }



    //============================================================== 课外作业

    @ApiOperation(value = "查询已发布的产品模型信息")
    @PostMapping("/QueryThingModelPublished")
    public ResponseResult<QueryThingModelVo> queryThingModelPublished(@RequestBody QueryThingModelPublishedRequest request){
        log.info("查询指定设备的物模型运行状态:{}",request);
        QueryThingModelVo thingModelPublished = deviceService.queryThingModelPublished(request);
        return ResponseResult.success(thingModelPublished);
    }



    @ApiOperation(value = "查询指定设备的物模型运行状态")
    @PostMapping("/QueryDevicePropertyStatus")
    public ResponseResult<DevicePropertyStatusVo> queryDevicePropertyStatus(@RequestBody DeviceDto dto){
        log.info("查询指定设备的物模型运行状态:{}",dto);
        DevicePropertyStatusVo devicePropertyStatusVo = deviceService.queryDevicePropertyStatus(dto);
        return ResponseResult.success(devicePropertyStatusVo);
    }



    @ApiOperation(value = "修改设备备注名称")
    @PostMapping("/UpdateDevice")
    public ResponseResult<Void> updateDevice(@RequestBody DeviceDto dto){
        log.info("修改设备备注名称:{}",dto);
        deviceService.updateDevice(dto);
        return ResponseResult.success();
    }



    @ApiOperation(value = "删除设备")
    @DeleteMapping("/DeleteDevice")
    public ResponseResult<Void> deleteDevice(@RequestBody DeviceDto dto){
        log.info("删除设备:{}",dto);
        deviceService.deleteDevice(dto);
        return ResponseResult.success();
    }

}
